home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / sound / sample20.zip / SAMPLASM.ASM < prev    next >
Assembly Source File  |  1989-05-20  |  23KB  |  769 lines

  1. ;
  2. ;   SAMPLASM.ASM
  3. ;   ------------
  4. ;
  5. ; One voice sampler playback system using fractional addition       16/3/89
  6. ;
  7. ;   with optional patch to drive PC speaker using PWM
  8. ;
  9. ;   For use with SAMPLER.PAS  V2
  10. ;
  11. ;
  12. ;
  13. ;
  14. ;                   (C) Copyright 1989 by Rowan McKenzie
  15. ;
  16. ;                  You may copy these files  or use the source  code  only
  17. ;        for non-profit purposes. Please contact me if you wish to use any
  18. ;        part of the package for commercial purposes.
  19. ;
  20. ;
  21. ;                       Rowan McKenzie 
  22. ;                       35 Moore Ave,
  23. ;                       Croydon 3136
  24. ;                       Vic Australia
  25. ;
  26. ;
  27. ;
  28. ;   This code provides the assembler section of SAMPLER.PAS
  29. ;
  30. ;     This version uses the system timer for sample timing, and is therefore
  31. ;   clock speed independent, although the CPU must be able to keep up with
  32. ;   the interrupt rate (20kHz!).
  33. ;
  34. ;
  35. ;
  36. ; Notes:
  37. ; ------
  38. ;
  39. ;  To assemble the D to A converter version, set pwm equ false
  40. ;
  41. ;  To assemble the PWM version, set pwm equ true
  42. ;
  43. ;  The D/A version uses the system timer (at 20kHz) to synchronise output
  44. ;     samples (and to make the code clock speed independent)
  45. ;
  46. ;  The PWM version uses the system timer in the same way, but triggers the
  47. ;     speaker timer channel as a monostable whose pulse length is proportional
  48. ;     to the sample value. Note that time constants are all scaled assuming
  49. ;     20kHz sample rate. If another sample rate is used, these will need to be
  50. ;     recalculated. Also, faster sampling rates mean lower effective resolution
  51. ;     from the one shot because it is clocked at a fixed frequency. The current
  52. ;     setup gives the equivalent of a 6 bit d/a converter (I know that's not
  53. ;     very good, but it's better than most PC speakers can justify!)
  54. ;
  55. ;
  56. ;
  57. ;  Variables defined in Pascal source:
  58. ;  -----------------------------------
  59. ;
  60. ;  release is a boolean variable indicating whether key releases should
  61. ;          terminate sound playback
  62. ;
  63. ;  loop is a boolean variable indicating that the loop section of the sample
  64. ;          should be played continuously after playback is complete
  65. ;
  66. ;  daout is an integer variable containing the d/a converter port
  67. ;          (for D/A version)
  68. ;
  69. ;  increment is an integer variable containing a fractional increment constant which
  70. ;          determines the pitch of the note to be played
  71. ;
  72. ;  bufloop is an integer variable containing the offset of the start of the
  73. ;          loop section
  74. ;
  75. ;  bufend is an integer variable containing the offset of the end of the sample
  76. ;          (for when the sample code is implemented)
  77. ;
  78. ;  bufstart is an integer variable containing the offset of the start of the
  79. ;          sample
  80. ;
  81. ;  kbdmode is a boolean variable indicating whether keyboard service routine
  82. ;          should pass control to bios (false) or handle the key itself (true)
  83. ;
  84. ;  kbdflag is a byte variable to indicate a key has been pressed during replay
  85. ;
  86. ;  keyval is a byte variable which will contain the scan code of a key pressed
  87. ;          during replay
  88. ;
  89. ;  tconstant is a byte variable which sets the system timer rate (20kHz)
  90. ;
  91. ;  timer is a boolean variable which indicates the timer should be used
  92. ;          to determine how long notes should last
  93. ;
  94. ;  tinterval is an integer variable indicating how long a note should be
  95. ;          played for (no. interrupts mod modulus)
  96. ;
  97. ;  modulus is a byte variable which determines after how many interrupts the
  98. ;          tinterval counter should be decremented
  99. ;
  100. ;  song is a boolean variable indicating whether a song is being played
  101. ;
  102. ;  trigger is an integer variable describing the minimum input level required
  103. ;          to begin sampling
  104. ;
  105.  
  106.         page    255,132
  107. title sampler asm module
  108.  
  109. ; ===== Equates =====
  110.  
  111. true    equ     1
  112. false   equ     0
  113.  
  114. pwm     equ     false                   ;pwm version if true else d/a version
  115.  
  116. tdelay  equ     2                       ;delay constant between samples while
  117.                                         ; waiting for trigger
  118. lshiftr equ     170                     ;scan code for left shift release
  119. rshiftr equ     182
  120. tccont  equ     43h                     ;control reg of 8253 timer chip
  121. tccount equ     42h                     ;counter reg 2 (speaker)
  122.  
  123. speaker equ     97                      ;speaker port for PWM version
  124.  
  125. data     segment word public
  126. ;
  127. ; data
  128. ;
  129.  
  130.         extrn   tconstant:word,keyval:word,kbdflag:word,bufstart:word
  131.         extrn   bufend:word,bufloop:word,increment:word,daout:word,loop:word
  132.         extrn   release:word,timer:word,tinterval:word,modulus:word,song:word
  133.         extrn   trigger:word,kbdmode:word,quickexit:word,buffer:word
  134.         extrn   bufferw:word,bufflen:word
  135.  
  136. sysold  dd      ?                       ;temp storage for old system int vector
  137. sptemp  dw      ?                       ;storage for sp for quick exit
  138. duration dw     ?                       ;duration counter
  139. modul   db      ?                       ;local modulus counter
  140.  
  141. data    ends
  142.  
  143. code    segment byte public
  144.         assume  cs:code,ds:data
  145.  
  146.         public  restore,sample,initial,replay,replayt,scalewave,echo
  147.  
  148. kbdold  dd      ?                       ;temp storage for old kbd int vector
  149. dstemp  dw      ?                       ;holds ds incase it's needed
  150.  
  151.  
  152. ;************************************************************
  153. ;
  154. ;  Restore  - restore int vectors and exits
  155. ;
  156. ;************************************************************
  157.  
  158. restore proc near
  159.         cli                                     ;restore old kbd interrupt
  160.         push    ds
  161.         mov     dx,word ptr cs:[kbdold]
  162.         mov     ds,word ptr cs:[kbdold+2]
  163.         mov     ax,2509h
  164.         int     21h                             ;restore old kbd vector
  165.         pop     ds
  166.  
  167.         if    pwm
  168.         mov     al,0b6h
  169.         out     tccont,al               ;change timer to square wave gen mode
  170.  
  171.         in      al,speaker
  172.         and    al,0fch
  173.         out     speaker,al              ;speaker one shot disable
  174.         endif
  175.  
  176.         sti
  177.         ret
  178. restore endp
  179.  
  180. ;***********************************************************************
  181. ;
  182. ;  Initialise - save int vector contents, replace with my version
  183. ;
  184. ;***********************************************************************
  185.  
  186. initial proc near
  187.         cli
  188.         mov     cs:[dstemp],ds
  189.         mov     ax,3509h                        ;get old kbd int
  190.         int     21h
  191.         mov     word ptr cs:[kbdold],bx            ;save it
  192.         mov     word ptr cs:[kbdold+2],es
  193.         mov     dx,offset kbdint
  194.         push    ds
  195.         mov     ax,cs
  196.         mov     ds,ax
  197.         mov     ax,2509h
  198.         int     21h                             ;replace with mine
  199.         pop     ds
  200.         mov     byte ptr [kbdflag],0            ;no key activity
  201.  
  202.         if    pwm
  203.         mov     al,0b6h
  204.         out     tccont,al               ;change timer to square wave gen mode
  205.         mov     al,byte ptr[tconstant]
  206.     shr    al,1
  207.         out     tccount,al              ;produce mean equivalent dc voltage
  208.     xor    al,al
  209.         out     tccount,al
  210.         in      al,speaker
  211.         or      al,3
  212.         out     speaker,al              ;speaker one shot enable
  213.     else
  214.         mov     dx,[daout]
  215.         mov    al,127
  216.     out    dx,al
  217.         endif
  218.  
  219.         sti
  220.         ret
  221. initial endp
  222.  
  223.  
  224. ;******************************************************************************
  225. ;
  226. ;  Sample data from A/D port
  227. ;
  228. ;******************************************************************************
  229.  
  230. sample  proc near
  231.         cli
  232.  
  233.         in      al,21h
  234.         or      al,98h                      ;disable comms, printer interrupts
  235.         out     21h,al
  236.  
  237.         mov     ax,3508h
  238.         int     21h                             ;get system timer vector
  239.         mov     word ptr [sysold],bx
  240.         mov     word ptr [sysold+2],es          ;and save it
  241.  
  242.         mov     dx,offset sysint
  243.         push    ds
  244.         mov     ax,cs
  245.         mov     ds,ax
  246.         mov     ax,2508h
  247.